<?php
class Tongji extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model('tongji_model');
		$this->load->helper('date');
		$this->load->helper('url');
		$this->load->library('session');

	}
	public function index()
	{
 		$this->load->view('tongji/register');
	}

	//网站列表
	public function websitelist()
	{
		$header['title'] = '统计服务';
		$data['userInfo'] = $this->tongji_model->getUserInfo();
		$data['websitelist'] = $this->tongji_model->getUserWebsite();
 		$this->load->view('main/websitelist',$data);
	}
	//主页
	public function main()
	{

		$header['title'] = '统计服务';
		$header['location1'] = '统计概况';
		$header['location2'] = '统计概况';

		$this->tongji_model->getWebSiteInfo();
		$sdate = $edate = date('Y-m-d');

		$arr = $this->tongji_model->DayData($sdate,$edate);
		$data['xml'] = $arr['xml'];
		for($i=0;$i<2;$i++)
		{
			$search_sdate = date('Y-m-d',strtotime("-".$i." day"));
			$data['gaikuang'][$search_sdate] = $this->tongji_model->gaikuang($search_sdate);
		}
		$total = $this->tongji_model->gaikuang();
		$data['total'] = $total;
		$data['today'] = $data['gaikuang'][date('Y-m-d')];
		$data['yestoday'] = $data['gaikuang'][date('Y-m-d',strtotime("-1 day"))];
		$data['userInfo'] = $this->tongji_model->getUserInfo();
		$data['top_url'] = $this->tongji_model->top_url($sdate,$edate);
		$data['top_area'] = $this->tongji_model->top_area($sdate,$edate);

 		$this->load->view('main/header',$header);
 		$this->load->view('main/index',$data);
 		$this->load->view('main/footer',$data);
	}
	//统计概况
	public function gaikuang()
	{
		Tongji::main();
	}
//按天查看访问详情
	public function showsomeday()
	{
		$search_day = intval($this->input->get("search_day")?$this->input->get("search_day"):0);
		$header['location1'] = '统计概况';
		if($search_day<1)
		{
			$header['title'] = $header['location2'] = '今日访问明细';
		}else{
			$header['title'] = $header['location2'] = '最近'.$search_day.'访问明细';
		}
		$tmp_sdate = $this->input->post("search_sdate");
		if($tmp_sdate)
		{
			$data['search_sdate'] = $tmp_sdate;
			$data['search_edate'] = htmlspecialchars($this->input->post("search_edate")?$this->input->post("search_edate"):date('Y-m-d'));
			$search_day = -1;
		}else{
			$data['search_sdate'] = htmlspecialchars($search_day==0?date('Y-m-d'):date('Y-m-d',strtotime('-'.$search_day.' day')));
			if($search_day == 1)
			{
				$data['search_edate'] = $data['search_sdate'];
			}else{
				$data['search_edate'] = htmlspecialchars($this->input->post("search_edate")?$this->input->post("search_edate"):date('Y-m-d'));
			}
		}

		$result_arr = $this->tongji_model->DayData($data['search_sdate'],$data['search_edate'],$search_day);
		$data['data'] = $result_arr['data'];
		$data['total'] = $result_arr['total'];
		$data['xml'] = $result_arr['xml'];
		$data['daydata'] = $result_arr['date_total'];
		$data['search_day'] = $search_day;
 		$this->load->view('main/header',$header);
 		$this->load->view('main/showsomeday',$data);
 		$this->load->view('main/footer',$data);
	}

	/*
	 * 添加网站
	 */
	public function addwebsite()
	{
		$this->load->library('Tools');
		$addwebsite = $this->input->post('addwebsite');
		if($addwebsite)
		{
			$flag = $this->tongji_model->save_website();
			if($flag)
			{
				$this->tools->showMessage("添加成功",'../user/usermessage');
			}
		}else{
			$data['websiteType'] = $this->tools->websiteType();
			$this->load->view('main/add_website',$data);
		}
	}
}
?>
